package com.atguigu.juc.chap02;

import java.util.concurrent.locks.ReentrantLock;

public class DeadlockDemo01 {

    private static final ReentrantLock lock01 = new ReentrantLock();
    private static final ReentrantLock lock02 = new ReentrantLock();

    public static void main(String[] args) {
        // 线程A
        new Thread(() -> {

            lock01.lock();
            System.out.println("A持有锁1，想要获取锁2");

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            lock02.lock();
            System.out.println("A持有锁1和锁2");

            lock01.unlock();
            System.out.println("A释放了锁1");

            lock02.unlock();
            System.out.println("A释放了锁2，A不再持有锁");

        }, "A").start();

        // 线程B
        new Thread(() -> {

            lock02.lock();
            System.out.println("B持有锁2，想要获取锁1");

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


            lock01.lock();
            System.out.println("B持有锁1和锁2");

            lock02.unlock();
            System.out.println("B释放了锁2");

            lock01.unlock();
            System.out.println("B释放了锁1，B不再持有锁");

        }, "B").start();
    }

}